package com.goodsogood.union.auth.web.validator;

import com.goodsogood.union.auth.conf.LogConfiger;
import com.goodsogood.union.auth.constant.KeyConst;
import com.goodsogood.union.auth.redis.RedisPool;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

/**
 * Created by dongguochao on 16/12/2016.
 */
@Component
public class UserPwdValidator {
    private static final Logger logger = LoggerFactory.getLogger(LogConfiger.APP);
    private static final int EXPIRE_TIME = 86400;      // 24小时
    @Autowired
    private RedisPool redisPool;
    public static final int PWD_WRONG_TIMES = 4;

    /**
     * 密码错误次数是否超过
     *
     * @param userPhone
     * @return
     */
    public boolean pwdWrongTimeOut(String userPhone) {
        String prefix = KeyConst.KEY_USER_PWDTIMEOUT;
        Jedis jedis = null;
        try {
            jedis = redisPool.getResource();
            
            String times = jedis.get(prefix + userPhone);
            if (!StringUtils.isNumeric(times)) {
                return false;
            }
            int ti = Integer.valueOf(times);
            if (ti >= PWD_WRONG_TIMES) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            logger.error("pwdWrongTimeOut happen error , userPhone is {}, error is ", userPhone, e);
            return false;
        } finally {
            redisPool.returnResource(jedis);
        }
    }

    public String pwdWrong(String userPhone) {
        String prefix = KeyConst.KEY_USER_PWDTIMEOUT;;
        Jedis jedis = null;
        try {
            jedis = redisPool.getResource();
            String key = prefix + userPhone;
            String times = jedis.get(key);
            if (!StringUtils.isNumeric(times)) {
                times = "0";
                logger.debug("密码错误【" + userPhone + "】 1次 ");
                jedis.set(key, "1");
                jedis.expire(key, EXPIRE_TIME);
            } else {
                int ti = Integer.valueOf(times);
                ti++;
                logger.debug("密码错误【" + userPhone + "】 " + ti + "次 ");
                jedis.set(key, String.valueOf(ti));
            }
            return times;
        } catch (Exception e) {
            logger.error("pwdWrong happen error , userPhone is {}, error is ", userPhone, e);
            return null;
        } finally {
            redisPool.returnResource(jedis);
        }
    }

    public void pwdOK(String userPhone) {
        Jedis jedis = null;
        try {
            jedis = redisPool.getResource();
            String prefix = KeyConst.KEY_USER_PWDTIMEOUT;;
            String key = prefix + userPhone;
            jedis.del(key);
        } catch (Exception e) {
            logger.error("pwdOK happen error , userPhone is {}, error is ", userPhone, e);
        } finally {
            redisPool.returnResource(jedis);
        }
    }
}